const connection = require('../app/database')
class UserService {
  async getUserByName(name) {
    const statement = `SELECT * FROM USER WHERE NAME = ?;`
    const result = await connection.execute(statement, [name])
    return result[0]
  }
  async create(user) {
    // 将用户信息保存到数据库中
    const statement = `INSERT INTO user (name, password) VALUES (?, ?);`
    const result = await connection.execute(statement, [user.name, user.password])
    return result[0]
  }

  async updateAvatarByuserId(avatarUrl, userId) {
    const statement = `UPDATE user SET avatar_url = ? WHERE id = ?;`
    const result = await connection.execute(statement, [avatarUrl, userId])
    return result
  }

  async getAvatarInfoByuserId(userId) {
    const statement = `SELECT * FROM avatar WHERE user_id = ?;`
    const [result] = await connection.execute(statement, [userId])
    return result[0]
  }
}

module.exports = new UserService()
